package com.sc.nft.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.sc.nft.dao.*;
import com.sc.nft.entity.*;
import com.sc.nft.entity.dto.ConfirmExchangeSubscribeDTO;
import com.sc.nft.entity.dto.ProductFirstDTO;
import com.sc.nft.entity.dto.RushBuyDTO;
import com.sc.nft.entity.vo.*;
import com.sc.nft.entity.vo.salvageEvents.SalvageProductPageVO;
import com.sc.nft.enums.*;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.pool.UserPool;
import com.sc.nft.service.AppVersionService;
import com.sc.nft.service.EquityPropsService;
import com.sc.nft.service.NftMallService;
import com.sc.nft.service.UserEquityPropsService;
import com.sc.nft.sup.ErrorCode;
import com.sc.nft.util.SubscribeCodeUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.api.RSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 商城业务
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class NftMallServiceImpl implements NftMallService {
    private final DigitalCollectionProductInfoDao digitalCollectionProductInfoDao;

    private final UserCreatorInfoDao userCreatorInfoDao;

    private final StaticDataDao staticDataDao;

    private final BoxItemDao boxItemDao;

    private final CatenaInfoDao catenaInfoDao;

    private final ProductStockDao productStockDao;

    private final UserCollectionDao userCollectionDao;

    private final ReceiveProductRecordDao receiveProductRecordDao;

    private final BuyFirstCollectionDao buyFirstCollectionDao;

    private final BuyFirstUserDao buyFirstUserDao;

    private final FirstBuyRecordDao firstBuyRecordDao;

    private final OrderDao orderDao;

    private final PrivilegeDao privilegeDao;

    private final PrivilegeUseLogDao privilegeUseLogDao;

    private final RushBuyCollectionDao rushBuyCollectionDao;

    private final RushBuyRecordDao rushBuyRecordDao;

    private final RushBuyUserDao rushBuyUserDao;

    private final AppVersionService appVersionService;

    private final BuyCollectionDiscountDao buyCollectionDiscountDao;

    private final BuyUserDiscountDao buyUserDiscountDao;

    private final DigitalCollectionDao digitalCollectionDao;

    private final UserEquityPropsService userEquityPropsService;

    private final EquityPropsService equityPropsService;

    private final DigitalCollectionProductInfoSubscribeDao digitalCollectionProductInfoSubscribeDao;

    private final DigitalCollectionProductInfoConditionDao digitalCollectionProductInfoConditionDao;

    private final DigitalCollectionProductInfoFreeSubscribeDao digitalCollectionProductInfoFreeSubscribeDao;

    private final Redisson redisson;

    private final DigitalCollectionProductInfoSubscribeCodeDao digitalCollectionProductInfoSubscribeCodeDao;

    private final UserInfoDao userInfoDao;

    private final DigitalCollectionProductInfoSubscribeWinBallotDao digitalCollectionProductInfoSubscribeWinBallotDao;

    private final DigitalCollectionProductInfoAuctionDao digitalCollectionProductInfoAuctionDao;
    private final DigitalCollectionProductInfoAuctionUserBiddingDao digitalCollectionProductInfoAuctionUserBiddingDao;
    private final DigitalCollectionProductInfoAuctionUserBiddingResultsDao digitalCollectionProductInfoAuctionUserBiddingResultsDao;
    private final DigitalCollectionProductInfoAuctionUserAgreementDao digitalCollectionProductInfoAuctionUserAgreementDao;

    @Override
    public Page<DigitalCollectionProductVO> getMallPageList(Long catenaId, String count, String num, String time, Integer typeClassify, Integer pageNo, Integer pageSize, String appStoreVersion) {
        UserInfo user = UserPool.getUser();
        //判断当前用户是否是苹果审核的用户
        int isIosShow = getIsIosAudit(user, appStoreVersion);

        Page<DigitalCollectionProductVO> mallPageList = digitalCollectionProductInfoDao.getMallPageList(new Page(pageNo, pageSize), catenaId, typeClassify, num, count, time, isIosShow);
        List<DigitalCollectionProductVO> records = mallPageList.getRecords();
        for (DigitalCollectionProductVO digitalCollectionProductVO : records) {
            List timeList = new ArrayList();
            if (digitalCollectionProductVO.getShowLevel() == 0) {
                digitalCollectionProductVO.setShowLevelName("");
            }
            if (ObjectUtil.isNotEmpty(digitalCollectionProductVO.getBeginTime())) {
                Date date = DateUtil.parse(digitalCollectionProductVO.getBeginTime(), "yyyy-MM-dd HH:mm:ss");
                digitalCollectionProductVO.setBeginTime(DateUtil.format(date, "MM-dd HH:mm"));
                String format = DateUtil.format(date, "MM-dd HH:mm");
                timeList.add(format + " 发售");
            }
            if (ObjectUtil.isNotEmpty(digitalCollectionProductVO.getPriorityPurchaseBeginTime())) {
                Date date = DateUtil.parse(digitalCollectionProductVO.getPriorityPurchaseBeginTime(), "yyyy-MM-dd HH:mm:ss");
                digitalCollectionProductVO.setPriorityPurchaseBeginTime(DateUtil.format(date, "MM-dd HH:mm"));
                String format = DateUtil.format(date, "MM-dd HH:mm");
                timeList.add(format + " 优先购");
            }
            if (ObjectUtil.isNotEmpty(digitalCollectionProductVO.getRushBuyBeginTime())) {
                Date date = DateUtil.parse(digitalCollectionProductVO.getRushBuyBeginTime(), "yyyy-MM-dd HH:mm:ss");
                digitalCollectionProductVO.setRushBuyBeginTime(DateUtil.format(date, "MM-dd HH:mm"));
                String format = DateUtil.format(date, "MM-dd HH:mm");
                timeList.add(format + " 优先抢");
            }
            digitalCollectionProductVO.setList(timeList);
        }
        return mallPageList;
    }

    /**
     * 判断当前用户是否是苹果审核的
     *
     * @param user
     * @return
     */
    private int getIsIosAudit(UserInfo user, String appStoreVersion) {
        int isIosShow = 0;
        if (user.getUserTel().equals("13849904109")) {
            isIosShow = 1;
        }

        if (StrUtil.isNotBlank(appStoreVersion)) {
            AppVersion ios = appVersionService.findOneByType("ios");
            if (ios.getAppStoreVersion().equals(appStoreVersion)) {
                isIosShow = 1;
            }
        }
        return isIosShow;
    }


    @Override
    public ProductDetailsVO getProductDetails(Long productId, String appStoreVersion) {
        ProductDetailsVO defaultProductDetails = getDefaultProductDetails(productId, appStoreVersion);
        String productShareUrl = staticDataDao.getByType("product_share_url").getValue();
        defaultProductDetails.setProductShareUrl(productShareUrl + "?productId=" + productId);
        defaultProductDetails.setLockedPositionDescribe();
        return defaultProductDetails;
    }

    @Override
    public SubscribeProductDetailsVO getSubscribeProductDetails(Long productId, String appStoreVersion) {
        SubscribeProductDetailsVO defaultProductDetails = getDefaultSubscribeProductDetails(productId, appStoreVersion);
        String productShareUrl = staticDataDao.getByType("product_share_url").getValue();
        defaultProductDetails.setProductShareUrl(productShareUrl + "?productId=" + productId);
        defaultProductDetails.setLockedPositionDescribe();
        return defaultProductDetails;
    }

    /**
     * 获取拍卖数字藏品详情
     *
     * @param productId
     * @param appStoreVersion
     * @return
     */
    @Override
    public AuctionProductDetailsVO getAuctionProductDetails(Long productId, String appStoreVersion) {
        AuctionProductDetailsVO defaultProductDetails = getDefaultAuctionProductDetails(productId, appStoreVersion);
        String productShareUrl = staticDataDao.getByType("product_share_url").getValue();
        defaultProductDetails.setProductShareUrl(productShareUrl + "?productId=" + productId);
        defaultProductDetails.setLockedPositionDescribe();
        return defaultProductDetails;
    }

    @Override
    public BlindBoxDetailsVO getBlindBoxDetails(Long productId, String appStoreVersion) {
        BlindBoxDetailsVO defaultBlindBoxDetails = getDefaultBlindBoxDetails(productId, appStoreVersion);
        String productShareUrl = staticDataDao.getByType("blind_box_share_url").getValue();
        defaultBlindBoxDetails.setProductShareUrl(productShareUrl + "?productId=" + productId);
        return defaultBlindBoxDetails;
    }

    @Override
    public BlindBoxDetailsVO getAllBlindBoxDetails(Long productId, String appStoreVersion) {
        BlindBoxDetailsVO defaultBlindBoxDetails = getBoxPoolBoxDetails(productId, appStoreVersion);
        String productShareUrl = staticDataDao.getByType("blind_box_share_url").getValue();
        defaultBlindBoxDetails.setProductShareUrl(productShareUrl + "?productId=" + productId);
        return defaultBlindBoxDetails;
    }


    @Override
    public Page<DigitalCollectionProductVO> searchProductList(String firstTitle, Integer typeClassify, Integer pageNo, Integer pageSize, String appStoreVersion) {
        UserInfo user = UserPool.getUser();
        int isIosShow = 0;
        if (ObjectUtil.isNotEmpty(user)) {
            if (user.getUserTel().equals("13849904109")) {
                isIosShow = 1;
            }
        }

        if (StrUtil.isNotBlank(appStoreVersion)) {
            AppVersion ios = appVersionService.findOneByType("ios");
            if (ios.getAppStoreVersion().equals(appStoreVersion)) {
                isIosShow = 1;
            }
        }

        Page<DigitalCollectionProductVO> digitalCollectionProductVOPage = digitalCollectionProductInfoDao.searchProductList(new Page(pageNo, pageSize), firstTitle, typeClassify, isIosShow);
        List<DigitalCollectionProductVO> records = digitalCollectionProductVOPage.getRecords();
        for (DigitalCollectionProductVO digitalCollectionProductVO : records) {
            List timeList = new ArrayList();
            if (digitalCollectionProductVO.getShowLevel() == 0) {
                digitalCollectionProductVO.setShowLevelName("");
            }
            if (ObjectUtil.isNotEmpty(digitalCollectionProductVO.getBeginTime())) {
                Date date = DateUtil.parse(digitalCollectionProductVO.getBeginTime(), "yyyy-MM-dd HH:mm:ss");
                digitalCollectionProductVO.setBeginTime(DateUtil.format(date, "MM-dd HH:mm"));
                String format = DateUtil.format(date, "MM-dd HH:mm");
                timeList.add(format + " 发售");
            }
            if (ObjectUtil.isNotEmpty(digitalCollectionProductVO.getPriorityPurchaseBeginTime())) {
                Date date = DateUtil.parse(digitalCollectionProductVO.getPriorityPurchaseBeginTime(), "yyyy-MM-dd HH:mm:ss");
                digitalCollectionProductVO.setPriorityPurchaseBeginTime(DateUtil.format(date, "MM-dd HH:mm"));
                String format = DateUtil.format(date, "MM-dd HH:mm");
                timeList.add(format + " 优先购");
            }
            if (ObjectUtil.isNotEmpty(digitalCollectionProductVO.getRushBuyBeginTime())) {
                Date date = DateUtil.parse(digitalCollectionProductVO.getRushBuyBeginTime(), "yyyy-MM-dd HH:mm:ss");
                digitalCollectionProductVO.setRushBuyBeginTime(DateUtil.format(date, "MM-dd HH:mm"));
                String format = DateUtil.format(date, "MM-dd HH:mm");
                timeList.add(format + "优先抢");
            }
            digitalCollectionProductVO.setList(timeList);
        }
        return digitalCollectionProductVOPage;

    }

    @Override
    public List<CatenaInfo> getCatenaListByType(Integer type) {
        List<CatenaInfo> list = catenaInfoDao.seriesList(type);
        return list;
    }

    @Override
    public Page<SalvageProductPageVO> getSalvageProductPage(NFTProductShowSalvagePrefectureEnum showSalvagePrefecture, Integer pageNo, Integer pageSize) {
        return digitalCollectionProductInfoDao.getSalvageProductPage(showSalvagePrefecture, pageNo, pageSize);
    }

    @Override
    public ProductDetailsVO getSalvageProductDetails(Long productId, String appStoreVersion) {
        ProductDetailsVO vo = getPrivateSalvageProductDetails(productId, appStoreVersion);
        String productShareUrl = staticDataDao.getByType("product_share_url").getValue();
        vo.setProductShareUrl(productShareUrl + "?productId=" + productId);
        return vo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void confirmExchangeSubscribe(ConfirmExchangeSubscribeDTO confirmExchangeSubscribeDTO) {
        UserInfo user = UserPool.getUser();
        //region 生成申购码
        RLock lock = redisson.getLock(RedisKeyEnum.SUBSCRIBE_CODE_LOCK.getKey() + user.getId());
        try {
            boolean res = lock.tryLock(10, TimeUnit.SECONDS);
            if (res) {
                if (confirmExchangeSubscribeDTO.getQuantity() <= 0) {
                    throw new GlobalRunTimeException("兑换数量必须大于0");
                }

                // 获取指定id上架中的数字藏品的普通商品,商品有可能是ios审核商品
                DigitalCollectionProductInfo digitalCollectionProductInfo = digitalCollectionProductInfoDao.getSubscribeProductDetails(confirmExchangeSubscribeDTO.getProductId(), 0);
                if (ObjectUtil.isEmpty(digitalCollectionProductInfo)) {
                    throw new GlobalRunTimeException(ErrorCode.PRODUCT_NOT_FOUND);
                }
                if (digitalCollectionProductInfo.getCommodityType() != CommodityType.SUBSCRIBE_COMMODITY) {
                    throw new GlobalRunTimeException("只支持申购商品");
                }
                // 创作者Id
                Long userCreatorId = digitalCollectionProductInfo.getUserCreatorId();
                // 根据创作者Id获取创作者信息
                UserCreatorInfo userCreatorInfo = userCreatorInfoDao.getById(userCreatorId);
                if (ObjectUtil.isEmpty(userCreatorInfo)) {
                    throw new GlobalRunTimeException(ErrorCode.PRODUCT_HAS_BEEN_DESCONTINUED);
                }
                //region 申购商品配置
                DigitalCollectionProductInfoSubscribe digitalCollectionProductInfoSubscribe = digitalCollectionProductInfoSubscribeDao.getByProductInfoSubscribe(digitalCollectionProductInfo.getId());
                if (Objects.isNull(digitalCollectionProductInfoSubscribe)) {
                    throw new GlobalRunTimeException("申购商品配置不存在");
                }
                // 兑换条件主信息
                confirmExchangeSubscribeDTO.setDigitalCollectionProductInfoSubscribe(digitalCollectionProductInfoSubscribe);
                // 申购时间判定
                confirmExchangeSubscribeDTO.subscribeTimeDecide();
                //最大兑换数量
                confirmExchangeSubscribeDTO.setMaxSubscribeNum(digitalCollectionProductInfoSubscribe.getMaxSubscribeNum());
                //已兑换数量
                Integer redeemedQuantity = digitalCollectionProductInfoSubscribeCodeDao.getCountByUserProductIdAndSchedule(user.getId(), confirmExchangeSubscribeDTO.getProductId(), null);
                confirmExchangeSubscribeDTO.setRedeemedQuantity(redeemedQuantity);
                //校验是否超过最大兑换数量
                confirmExchangeSubscribeDTO.validateMaxSubscribeNum();
                //endregion
                //region 是否需要参与条件
                if (digitalCollectionProductInfoSubscribe.getParticipationCondition()) {
                    //region 申购条件配置
                    List<DigitalCollectionProductInfoCondition> digitalCollectionProductInfoConditionList = digitalCollectionProductInfoConditionDao.getDigitalCollectionProductInfoConditionBatchByProductId(digitalCollectionProductInfo.getId());
                    confirmExchangeSubscribeDTO.setDigitalCollectionProductInfoConditionList(digitalCollectionProductInfoConditionList);
                    //endregion

                    // 条件真实性 在set进去digitalCollectionProductInfoConditionList时候,在这个里面就经过了效验
                    if (confirmExchangeSubscribeDTO.getSubscribeConditionAuthentic()) {
                        List<MyCollectionCountVO> userCollectionList = (Objects.nonNull(confirmExchangeSubscribeDTO.getConditionCollectionIds()) && confirmExchangeSubscribeDTO.getConditionCollectionIds().size() > 0) ? userCollectionDao.getByUserAndIdList(user.getId(), confirmExchangeSubscribeDTO.getConditionCollectionIds()) : Lists.newArrayList();
                        // 验证钩子,验证是否满足条件
                        confirmExchangeSubscribeDTO.conditionVerifyHook(userCollectionList);
                    }
                }
                //endregion
                //region 免费申购数量
                if (digitalCollectionProductInfoSubscribe.getFreeSubscribe()) {
                    List<DigitalCollectionProductInfoFreeSubscribe> digitalCollectionProductInfoFreeSubscribes = digitalCollectionProductInfoFreeSubscribeDao.getDigitalCollectionProductInfoFreeSubscribeAdmin(digitalCollectionProductInfo.getId());
                    confirmExchangeSubscribeDTO.setDigitalCollectionProductInfoFreeSubscribes(digitalCollectionProductInfoFreeSubscribes);
                    // 判定集合是否数据存在.
                    if (confirmExchangeSubscribeDTO.getFreeConditionAuthentic()) {
                        // 根据情况查询数据.
                        List<MyCollectionCountVO> userCollectionList =
                                (Objects.nonNull(confirmExchangeSubscribeDTO.getFreeCollectionIds()) && confirmExchangeSubscribeDTO.getFreeCollectionIds().size() > 0)
                                        ? userCollectionDao.getByUserAndIdList(user.getId(), confirmExchangeSubscribeDTO.getFreeCollectionIds())
                                        : Lists.newArrayList();
                        // 获取已经获得的免费申购码数量
                        confirmExchangeSubscribeDTO.setFreeSubscribeNum(digitalCollectionProductInfoSubscribeCodeDao.getCountByUserProductIdAndSourece(user.getId(), confirmExchangeSubscribeDTO.getProductId(), ProductInfoSubscribeSourceEnum.FREE));
                        // 当前用户免费的最大获取数据的免费申购码,直接get maxFreeSubscribeNum 字段就可以获取.
                        confirmExchangeSubscribeDTO.maxFreeSubscribeNUmAdd(userCollectionList);
                    }
                }
                //endregion 免费申购数量


                //region 设计需要申购数量
                Integer diffSubscribe = confirmExchangeSubscribeDTO.getQuantity() - confirmExchangeSubscribeDTO.getMaxFreeSubscribeNum();
                Integer requireEquityPropsStock = 0;
                if (diffSubscribe > 0) {
                    //拥有道具数
                    Integer equityPropsHaveNum = 0;
                    if (Objects.nonNull(digitalCollectionProductInfoSubscribe.getPropId())
                            && digitalCollectionProductInfoSubscribe.getProductId() > 0) {
                        equityPropsHaveNum = userEquityPropsService.getHavingCountByUserId(digitalCollectionProductInfoSubscribe.getPropId(), user.getId());
                    }
                    //所需道具库存
                    requireEquityPropsStock = diffSubscribe * digitalCollectionProductInfoSubscribe.getVoucherSum();
                    if (requireEquityPropsStock > equityPropsHaveNum) {
                        throw new GlobalRunTimeException("道具不足");
                    }
                } else {
                    requireEquityPropsStock = 0;
                }
                //endregion
                //申购码缓存
                RSet<String> subscribeCodeCache = redisson.getSet(RedisKeyEnum.SUBSCRIBE_CODE_LOCK.getKey() + confirmExchangeSubscribeDTO.getProductId());
                //过期时间：申购结束时间 + 10s
                subscribeCodeCache.expire(DateUtil.between(DateTime.now(), digitalCollectionProductInfoSubscribe.getSubscribeEndTime(), DateUnit.SECOND) + 10L, TimeUnit.SECONDS);

                //存放申购码结果
                Set<String> subscribeCodes = Sets.newHashSet();
                while (subscribeCodes.size() < confirmExchangeSubscribeDTO.getQuantity()) {
                    String subscribeCode = SubscribeCodeUtil.subscribeCodeString();
                    //校验并添加，原子操作，如果已存在，返回false
                    if(subscribeCodeCache.add(subscribeCode)) {
                        subscribeCodes.add(subscribeCode);
                    }
                }
                this.doConfirmExchangeSubscribe(requireEquityPropsStock,
                        digitalCollectionProductInfoSubscribe,
                        user,
                        subscribeCodes,
                        confirmExchangeSubscribeDTO.getProductId());
            } else {
                throw new GlobalRunTimeException("申购码申请频繁，请稍后重试");
            }
        } catch (Exception e) {
            log.error("申购码申请异常：", e);
            throw new GlobalRunTimeException(e.getMessage());
        } finally {
            lock.unlock();
        }
        //endregion
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void doConfirmExchangeSubscribe(Integer requireEquityPropsStock,
                                           DigitalCollectionProductInfoSubscribe digitalCollectionProductInfoSubscribe,
                                           UserInfo user,
                                           Set<String> subscribeCodes,
                                           Long productId) {
        List<Long> userEquityPropsIdList = Lists.newArrayList();
        if (requireEquityPropsStock > 0) {
            //需要申购兑换
            userEquityPropsIdList = userEquityPropsService.subVoucher(digitalCollectionProductInfoSubscribe.getPropId(), EquityPropsTypeEnum.VIRTUAL_PROPS, user.getId(), requireEquityPropsStock, "申购兑换", productId, UserEquityPropsUseTypeEnum.SUBSCRIBE_EXCHANGE);
        }
        int i = 0;
        for (String subscribeCode : subscribeCodes) {
            DigitalCollectionProductInfoSubscribeCode digitalCollectionProductInfoSubscribeCode = new DigitalCollectionProductInfoSubscribeCode();
            digitalCollectionProductInfoSubscribeCode.setUserId(user.getId());
            digitalCollectionProductInfoSubscribeCode.setProductId(productId);
            digitalCollectionProductInfoSubscribeCode.setSubscribeCode(subscribeCode);
            if (i < userEquityPropsIdList.size()) {
                //需要兑换
                digitalCollectionProductInfoSubscribeCode.setSourece(ProductInfoSubscribeSourceEnum.PROP);
                digitalCollectionProductInfoSubscribeCode.setPropId(digitalCollectionProductInfoSubscribe.getPropId());
                digitalCollectionProductInfoSubscribeCode.setUserEquityPropsId(userEquityPropsIdList.get(i));
                i = i + digitalCollectionProductInfoSubscribe.getVoucherSum();
            } else {
                //免费兑换
                digitalCollectionProductInfoSubscribeCode.setSourece(ProductInfoSubscribeSourceEnum.FREE);
                i = i + 1;
            }
            digitalCollectionProductInfoSubscribeCode.setSchedule(ProductInfoSubscribeScheduleEnum.WAIT_FOR_WIN);
            digitalCollectionProductInfoSubscribeCode.setCreateTime(DateTime.now());
            digitalCollectionProductInfoSubscribeCode.setModifyTime(DateTime.now());
            digitalCollectionProductInfoSubscribeCode.insert();
        }
    }

    @Override
    public Page<MySubscribeCodesWinDataVo> getSubscribeWin(Page page, Long productId) {
        Page<MySubscribeCodesWinDataVo> mySubscribeCodesDataVoPage = digitalCollectionProductInfoSubscribeWinBallotDao.getSubscribeWin(page, productId);
        return mySubscribeCodesDataVoPage;
    }

    private BlindBoxDetailsVO getDefaultBlindBoxDetails(Long productId, String appStoreVersion) {
        UserInfo user = UserPool.getUser();
        int isIosShow = 0;
        if (ObjectUtil.isNotEmpty(user)) {
            if (user.getUserTel().equals("13849904109")) {
                isIosShow = 1;
            }
        }
        if (StrUtil.isNotBlank(appStoreVersion)) {
            AppVersion ios = appVersionService.findOneByType("ios");
            if (ios.getAppStoreVersion().equals(appStoreVersion)) {
                isIosShow = 1;
            }
        }

        DigitalCollectionProductInfo digitalCollectionProductInfo = digitalCollectionProductInfoDao.getBlindBoxDetails(productId, isIosShow);
        if (ObjectUtil.isEmpty(digitalCollectionProductInfo) || BooleanUtil.isFalse(digitalCollectionProductInfo.getStatus())) {
            throw new GlobalRunTimeException(ErrorCode.PRODUCT_HAS_BEEN_DESCONTINUED);
        }

        BlindBoxDetailsVO blindBoxDetailsVO = BeanUtil.toBean(digitalCollectionProductInfo, BlindBoxDetailsVO.class);
        blindBoxDetailsVO.setProductId(productId);

        Date beginTime = digitalCollectionProductInfo.getBeginTime();
        if (ObjectUtil.isEmpty(beginTime)) {
            blindBoxDetailsVO.setBeginTime("");
        } else {
            String forDate = DateUtil.format(beginTime, "MM-dd HH:mm:ss");
            blindBoxDetailsVO.setBeginTime(forDate);
            blindBoxDetailsVO.setBuyBeginTime(beginTime);
        }

        ProductStock productStock = productStockDao.getProductStock(productId, digitalCollectionProductInfo.getRelationId(), null);
        if (ObjectUtil.isEmpty(productStock)) {
            throw new GlobalRunTimeException(ErrorCode.PRODUCT_HAS_BEEN_DESCONTINUED);
        }
        Integer sumCount = productStock.getSumCount();
        Integer usableCount = productStock.getUsableCount();


        Long userCreatorId = digitalCollectionProductInfo.getUserCreatorId();
        UserCreatorInfo userCreatorInfo = userCreatorInfoDao.getById(userCreatorId);
        if (ObjectUtil.isEmpty(userCreatorInfo)) {
            throw new GlobalRunTimeException(ErrorCode.PRODUCT_HAS_BEEN_DESCONTINUED);
        }
        StaticData staticData = staticDataDao.getByType("purchase_notes");
        blindBoxDetailsVO.setPurchaseNotes(staticData.getValue());
        blindBoxDetailsVO.setNickName(userCreatorInfo.getNickName());
        blindBoxDetailsVO.setCreatorAvatarImg(userCreatorInfo.getCreatorAvatarImg());

        List<BoxDigitalCollectionVO> boxDigitalCollectionVOS = boxItemDao.getDigitalCollections(digitalCollectionProductInfo.getRelationId());
        blindBoxDetailsVO.setList(boxDigitalCollectionVOS);


        CatenaInfo catenaInfo = catenaInfoDao.getById(digitalCollectionProductInfo.getCatenaId());
        if (ObjectUtil.isEmpty(catenaInfo)) {
            throw new GlobalRunTimeException(ErrorCode.PRODUCT_HAS_BEEN_DESCONTINUED);
        }

        if (usableCount <= 0) {
            blindBoxDetailsVO.setIsSoldOut(Boolean.TRUE);
            //商品状态: 1:立即购买 2:立即抢购 3:已售罄啦 4:暂未开始
            blindBoxDetailsVO.setProductType(3);
        }

        blindBoxDetailsVO.setProductCount(sumCount);
        blindBoxDetailsVO.setLaveNum(usableCount);
        blindBoxDetailsVO.setCatenaPresentation(catenaInfo.getPresentation());

        blindBoxDetailsVO.setFirstPay(false);
        blindBoxDetailsVO.setFirstTime(0);
        blindBoxDetailsVO.setUpperLimit(digitalCollectionProductInfo.getUpperLimit());
        DateTime now = DateTime.now();
        UserInfo loginUser = UserPool.getUser();
        blindBoxDetailsVO.setRushBuyNum(0);
        blindBoxDetailsVO.setIsRushBuy(Boolean.FALSE);
        //获取优先购信息
        List<ProductFirstDTO> productFirstDTOS = getBuyFirstList(digitalCollectionProductInfo, loginUser, now);
        List<RushBuyDTO> rushBuyList = Lists.newArrayList();
        if (productFirstDTOS.size() > 0) {
            int sumFirstPayTime = productFirstDTOS.stream().mapToInt(x -> x.getSumCount() - x.getUseCount()).sum();
            blindBoxDetailsVO.setFirstTime(Math.min(sumFirstPayTime, digitalCollectionProductInfo.getPriorityPurchaseLimit()));
            if (blindBoxDetailsVO.getFirstTime() > 0) {
                blindBoxDetailsVO.setFirstPay(true);
                blindBoxDetailsVO.setFirstDTOS(productFirstDTOS);
            }
            blindBoxDetailsVO.setFirstDTOS(productFirstDTOS.stream().sorted(Comparator.comparing(ProductFirstDTO::getType)).collect(Collectors.toList()));
        } else {
            blindBoxDetailsVO.setFirstDTOS(productFirstDTOS);

            //优先抢逻辑
            rushBuyList = getRushBuyList(digitalCollectionProductInfo, loginUser, now);
            if (rushBuyList.size() > 0) {
                int rushBuyNum = rushBuyList.stream().mapToInt(x -> x.getSumCount() - x.getUseCount()).sum();
                blindBoxDetailsVO.setRushBuyNum(rushBuyNum);
                if (blindBoxDetailsVO.getRushBuyNum() > 0) {
                    blindBoxDetailsVO.setIsRushBuy(Boolean.TRUE);
                }
                blindBoxDetailsVO.setRushBuyDTOS(rushBuyList.stream().sorted(Comparator.comparing(RushBuyDTO::getType)).collect(Collectors.toList()));
            } else {
                blindBoxDetailsVO.setRushBuyDTOS(rushBuyList);
            }
        }
        //App应该显示折扣后的价格
        BigDecimal minDiscount = new BigDecimal("1");
        if (ObjectUtil.isNotEmpty(user)) {
            minDiscount = getMinDiscount(user.getId(), productId);
        }
        BigDecimal productNum = digitalCollectionProductInfo.getProductNum();
        BigDecimal bigDecimal = productNum.multiply(minDiscount).setScale(2, BigDecimal.ROUND_DOWN);
        blindBoxDetailsVO.setProductNum(bigDecimal.toPlainString());

        blindBoxDetailsVO.setIsRegisterTime(digitalCollectionProductInfo.getIsRegisterTime());
        blindBoxDetailsVO.setRegisterBeginTime(digitalCollectionProductInfo.getRegisterBeginTime());
        blindBoxDetailsVO.setRegisterEndTime(digitalCollectionProductInfo.getRegisterEndTime());


        if (digitalCollectionProductInfo.getIsRegisterTime()) {
            if (user.getCreateTime().after(digitalCollectionProductInfo.getRegisterBeginTime()) && user.getCreateTime().before(digitalCollectionProductInfo.getRegisterEndTime())) {
                blindBoxDetailsVO.setIsConforRegisterTime(Boolean.TRUE);
            }
        }

        return blindBoxDetailsVO;
    }


    private BlindBoxDetailsVO getBoxPoolBoxDetails(Long productId, String appStoreVersion) {
        UserInfo user = UserPool.getUser();
        int isIosShow = 0;
        if (ObjectUtil.isNotEmpty(user)) {
            if (user.getUserTel().equals("13849904109")) {
                isIosShow = 1;
            }
        }
        if (StrUtil.isNotBlank(appStoreVersion)) {
            AppVersion ios = appVersionService.findOneByType("ios");
            if (ios.getAppStoreVersion().equals(appStoreVersion)) {
                isIosShow = 1;
            }
        }

        DigitalCollectionProductInfo digitalCollectionProductInfo = digitalCollectionProductInfoDao.getAllBlindBoxDetails(productId, isIosShow);
        if (ObjectUtil.isEmpty(digitalCollectionProductInfo)) {
            throw new GlobalRunTimeException(ErrorCode.PRODUCT_HAS_BEEN_DESCONTINUED);
        }

        BlindBoxDetailsVO blindBoxDetailsVO = BeanUtil.toBean(digitalCollectionProductInfo, BlindBoxDetailsVO.class);
        blindBoxDetailsVO.setProductId(productId);

        Date beginTime = digitalCollectionProductInfo.getBeginTime();
        if (ObjectUtil.isEmpty(beginTime)) {
            blindBoxDetailsVO.setBeginTime("");
        } else {
            String forDate = DateUtil.format(beginTime, "MM-dd HH:mm:ss");
            blindBoxDetailsVO.setBeginTime(forDate);
        }

        ProductStock productStock = productStockDao.getProductStock(productId, digitalCollectionProductInfo.getRelationId(), null);
        if (ObjectUtil.isEmpty(productStock)) {
            throw new GlobalRunTimeException(ErrorCode.PRODUCT_HAS_BEEN_DESCONTINUED);
        }
        Integer sumCount = productStock.getSumCount();
        Integer usableCount = productStock.getUsableCount();


        Long userCreatorId = digitalCollectionProductInfo.getUserCreatorId();
        UserCreatorInfo userCreatorInfo = userCreatorInfoDao.getById(userCreatorId);
        if (ObjectUtil.isEmpty(userCreatorInfo)) {
            throw new GlobalRunTimeException(ErrorCode.PRODUCT_HAS_BEEN_DESCONTINUED);
        }
        StaticData staticData = staticDataDao.getByType("purchase_notes");
        blindBoxDetailsVO.setPurchaseNotes(staticData.getValue());
        blindBoxDetailsVO.setNickName(userCreatorInfo.getDaoName());
        blindBoxDetailsVO.setCreatorAvatarImg(userCreatorInfo.getCreatorAvatarImg());

        List<BoxDigitalCollectionVO> boxDigitalCollectionVOS = boxItemDao.getDigitalCollections(digitalCollectionProductInfo.getRelationId());
        blindBoxDetailsVO.setList(boxDigitalCollectionVOS);


        CatenaInfo catenaInfo = catenaInfoDao.getById(digitalCollectionProductInfo.getCatenaId());
        if (ObjectUtil.isEmpty(catenaInfo)) {
            throw new GlobalRunTimeException(ErrorCode.PRODUCT_HAS_BEEN_DESCONTINUED);
        }

        if (usableCount <= 0) {
            blindBoxDetailsVO.setIsSoldOut(Boolean.TRUE);
            //商品状态: 1:立即购买 2:立即抢购 3:已售罄啦 4:暂未开始
            blindBoxDetailsVO.setProductType(3);
        }

        blindBoxDetailsVO.setProductCount(sumCount);
        blindBoxDetailsVO.setLaveNum(usableCount);
        blindBoxDetailsVO.setCatenaPresentation(catenaInfo.getPresentation());

        blindBoxDetailsVO.setFirstPay(false);
        blindBoxDetailsVO.setFirstTime(0);
//        blindBoxDetailsVO.setUpperLimit(digitalCollectionProductInfo.getUpperLimit());
        DateTime now = DateTime.now();
        UserInfo loginUser = UserPool.getUser();
        blindBoxDetailsVO.setRushBuyNum(0);
        blindBoxDetailsVO.setIsRushBuy(Boolean.FALSE);
        //获取优先购信息
        List<ProductFirstDTO> productFirstDTOS = getBuyFirstList(digitalCollectionProductInfo, loginUser, now);
        List<RushBuyDTO> rushBuyList = Lists.newArrayList();
        if (productFirstDTOS.size() > 0) {
            int sumFirstPayTime = productFirstDTOS.stream().mapToInt(x -> x.getSumCount() - x.getUseCount()).sum();
            blindBoxDetailsVO.setFirstTime(Math.min(sumFirstPayTime, digitalCollectionProductInfo.getPriorityPurchaseLimit()));
            if (blindBoxDetailsVO.getFirstTime() > 0) {
                blindBoxDetailsVO.setFirstPay(true);
                blindBoxDetailsVO.setFirstDTOS(productFirstDTOS);
            }
            blindBoxDetailsVO.setFirstDTOS(productFirstDTOS.stream().sorted(Comparator.comparing(ProductFirstDTO::getType)).collect(Collectors.toList()));
        } else {
            blindBoxDetailsVO.setFirstDTOS(productFirstDTOS);

            //优先抢逻辑
            rushBuyList = getRushBuyList(digitalCollectionProductInfo, loginUser, now);
            if (rushBuyList.size() > 0) {
                int rushBuyNum = rushBuyList.stream().mapToInt(x -> x.getSumCount() - x.getUseCount()).sum();
                blindBoxDetailsVO.setRushBuyNum(rushBuyNum);
                if (blindBoxDetailsVO.getRushBuyNum() > 0) {
                    blindBoxDetailsVO.setIsRushBuy(Boolean.TRUE);
                }
                blindBoxDetailsVO.setRushBuyDTOS(rushBuyList.stream().sorted(Comparator.comparing(RushBuyDTO::getType)).collect(Collectors.toList()));
            } else {
                blindBoxDetailsVO.setRushBuyDTOS(rushBuyList);
            }
        }
        //App应该显示折扣后的价格
        BigDecimal minDiscount = new BigDecimal("1");
        if (ObjectUtil.isNotEmpty(user)) {
            minDiscount = getMinDiscount(user.getId(), productId);
        }
        BigDecimal productNum = digitalCollectionProductInfo.getProductNum();
        BigDecimal bigDecimal = productNum.multiply(minDiscount).setScale(2, BigDecimal.ROUND_DOWN);
        blindBoxDetailsVO.setProductNum(bigDecimal.toPlainString());
        blindBoxDetailsVO.setBuyBeginTime(digitalCollectionProductInfo.getBeginTime());
        fillingRedeem(blindBoxDetailsVO, digitalCollectionProductInfo);
        return blindBoxDetailsVO;
    }

    public void fillingRedeem(BlindBoxDetailsVO vo, DigitalCollectionProductInfo info) {
        Date now = new Date();
        vo.setRedeem(1);
        //设置的开始和结束时间不为空，且当前时间不在两个时间段内则无法购买，历史老商品未配置时间段的则为永久出售不做限制 区分已结束、未开始
        if (null != info.getBeginTime() && null != info.getEndTime()) {
            if (now.before(info.getBeginTime())) {
                vo.setRedeem(0);
            }
            if (now.after(info.getEndTime())) {
                vo.setRedeem(2);
            }

        }
    }


    private ProductDetailsVO getDefaultProductDetails(Long productId, String appStoreVersion) {
        UserInfo user = UserPool.getUser();
        int isIosShow = 0;
        // 如果不为空,则判定账号是否为下方对应的之一.如果是的话就显示ios的
        if (ObjectUtil.isNotEmpty(user)) {
            if (user.getUserTel().equals("13849904109")) {
                isIosShow = 1;
            }
        }
        // 如果app的版本不为空的话,查找ios的版本,如果版本和ios当前版本一致,也显示ios的
        if (StrUtil.isNotBlank(appStoreVersion)) {
            AppVersion ios = appVersionService.findOneByType("ios");
            if (ios.getAppStoreVersion().equals(appStoreVersion)) {
                isIosShow = 1;
            }
        }
        // 获取指定id上架中的数字藏品的普通商品,商品有可能是ios审核商品
        DigitalCollectionProductInfo digitalCollectionProductInfo = digitalCollectionProductInfoDao.getProductDetails(productId, isIosShow);
        if (ObjectUtil.isEmpty(digitalCollectionProductInfo)) {
            throw new GlobalRunTimeException(ErrorCode.PRODUCT_NOT_FOUND);
        }
        // 创作者Id
        Long userCreatorId = digitalCollectionProductInfo.getUserCreatorId();
        // 根据创作者Id获取创作者信息
        UserCreatorInfo userCreatorInfo = userCreatorInfoDao.getById(userCreatorId);
        if (ObjectUtil.isEmpty(userCreatorInfo)) {
            throw new GlobalRunTimeException(ErrorCode.PRODUCT_HAS_BEEN_DESCONTINUED);
        }
        // 藏品/盲盒的数量的数据
        ProductStock productStock = productStockDao.getProductStock(productId, digitalCollectionProductInfo.getRelationId(), null);
        if (ObjectUtil.isEmpty(productStock)) {
            throw new GlobalRunTimeException(ErrorCode.PRODUCT_HAS_BEEN_DESCONTINUED);
        }
        Integer sumCount = productStock.getSumCount();
        Integer usableCount = productStock.getUsableCount();
        //获取权益介绍
        StaticData staticData = staticDataDao.getByType("purchase_notes");
        // 参数复制到返回值中
        ProductDetailsVO productDetailsVO = BeanUtil.toBean(digitalCollectionProductInfo, ProductDetailsVO.class);
        Date beginTime = digitalCollectionProductInfo.getBeginTime();
        // 如果权益为空的话,那么返回给前段要为"" 不可以为null  否则前段会炸
        if (ObjectUtil.isEmpty(productDetailsVO.getPrivilegePresentation())) {
            productDetailsVO.setPrivilegePresentation("");
        }

        // 如果抢购时间为null的话,也要返回""否则的话按照MM-dd HH:mm:ss进行返回
        if (ObjectUtil.isEmpty(beginTime)) {
            productDetailsVO.setBeginTime("");
        } else {
            String forDate = DateUtil.format(beginTime, "MM-dd HH:mm:ss");
            productDetailsVO.setBeginTime(forDate);
        }

        // 数量小于等于0的时候,告知售罄
        if (usableCount <= 0) {
            productDetailsVO.setIsSoldOut(Boolean.TRUE);
            //商品状态: 1:立即购买 2:立即抢购 3:已售罄啦 4:暂未开始
            productDetailsVO.setProductType(3);
        }

        //已卖出数量
        productDetailsVO.setAlreadyNum(sumCount - usableCount);
        productDetailsVO.setLaveNum(usableCount);
        //总发售数量
//        productDetailsVO.setProductCount(sumCount);
        //限量 = 藏品总库存
        if (digitalCollectionProductInfo.getTypeClassify() == ClassifyType.COLLECTION) {
            DigitalCollection digitalCollection = digitalCollectionDao.getById(digitalCollectionProductInfo.getRelationId());
            productDetailsVO.setProductCount(digitalCollection.getSumCount());
            productDetailsVO.setModelImg(digitalCollection.getModelImg());
            productDetailsVO.setSkipUrl(digitalCollection.getSkipUrl());
            productDetailsVO.setModelDistance(digitalCollection.getModelDistance());
        } else {
            productDetailsVO.setProductCount(productStock.getSumCount());
            productDetailsVO.setModelImg(digitalCollectionProductInfo.getModelImg());
            productDetailsVO.setSkipUrl(digitalCollectionProductInfo.getSkipUrl());
            productDetailsVO.setModelDistance(digitalCollectionProductInfo.getModelDistance());
        }

        productDetailsVO.setNickName(userCreatorInfo.getDaoName());

        productDetailsVO.setCreatorPresentation(userCreatorInfo.getCreatorPresentation());
        productDetailsVO.setCreatorAvatarImg(userCreatorInfo.getCreatorAvatarImg());
        productDetailsVO.setProductId(productId);
        productDetailsVO.setPurchaseNotes(staticData.getValue());
        productDetailsVO.setFirstPay(Boolean.FALSE);
        productDetailsVO.setFirstTime(0);
        productDetailsVO.setThreeModelId(digitalCollectionProductInfo.getThreeModelId());
        productDetailsVO.setRushBuyNum(0);
        productDetailsVO.setIsRushBuy(Boolean.FALSE);
        productDetailsVO.setUpperLimit(digitalCollectionProductInfo.getUpperLimit());
        DateTime now = DateTime.now();
        UserInfo loginUser = UserPool.getUser();
        List<ProductFirstDTO> productFirstDTOS;
        List<RushBuyDTO> rushBuyList = Lists.newArrayList();
        //优先购
        productFirstDTOS = getBuyFirstList(digitalCollectionProductInfo, loginUser, now);
        if (productFirstDTOS.size() > 0) {
            int sumFirstPayTime = productFirstDTOS.stream().mapToInt(x -> x.getSumCount() - x.getUseCount()).sum();
            productDetailsVO.setFirstTime(Math.min(sumFirstPayTime, digitalCollectionProductInfo.getPriorityPurchaseLimit()));
            if (productDetailsVO.getFirstTime() > 0) {
                productDetailsVO.setFirstPay(Boolean.TRUE);
            }
            productDetailsVO.setFirstDTOS(productFirstDTOS.stream().sorted(Comparator.comparing(ProductFirstDTO::getType)).collect(Collectors.toList()));
            productDetailsVO.setRushBuyDTOS(rushBuyList);
        } else {
            productDetailsVO.setFirstDTOS(productFirstDTOS);


            //优先抢逻辑
            rushBuyList = getRushBuyList(digitalCollectionProductInfo, loginUser, now);
            if (rushBuyList.size() > 0) {
                // 购买上限数量
                int rushBuyNum = digitalCollectionProductInfo.getUpperLimit();
                productDetailsVO.setRushBuyNum(rushBuyNum);
                if (productDetailsVO.getRushBuyNum() > 0) {
                    productDetailsVO.setIsRushBuy(Boolean.TRUE);
                }
                productDetailsVO.setRushBuyDTOS(rushBuyList.stream().sorted(Comparator.comparing(RushBuyDTO::getType)).collect(Collectors.toList()));
            } else {
                productDetailsVO.setRushBuyDTOS(rushBuyList);
            }
        }
        //App应该显示折扣后的价格
        BigDecimal minDiscount = new BigDecimal("1");
        if (ObjectUtil.isNotEmpty(user)) {
            minDiscount = getMinDiscount(user.getId(), productId);
        }
        BigDecimal productNum = digitalCollectionProductInfo.getProductNum();
        BigDecimal bigDecimal = productNum.multiply(minDiscount).setScale(2, BigDecimal.ROUND_DOWN);
        productDetailsVO.setProductNum(bigDecimal.toPlainString());
        String equityPropsName = "";
        Integer equityPropsNum = 0;
        Integer equityPropsHaveNum = 0;
        Long equityPropsId = digitalCollectionProductInfo.getEquityPropsId();
        if (ObjectUtil.isNotNull(equityPropsId) && equityPropsId.intValue() > 0) {
            //权益道具数量和记录
            equityPropsName = equityPropsService.getById(digitalCollectionProductInfo.getEquityPropsId()).getFirstTitle();
            equityPropsNum = digitalCollectionProductInfo.getEquityPropsNum();
            if (ObjectUtil.isNotNull(user)) {
                equityPropsHaveNum = userEquityPropsService.getHavingCountByUserId(digitalCollectionProductInfo.getEquityPropsId(), user.getId());
            } else {
                equityPropsHaveNum = 0;
            }
        }
        productDetailsVO.setEquityPropsName(equityPropsName);
        productDetailsVO.setEquityPropsNum(equityPropsNum);
        //查询用户持有的数量
        productDetailsVO.setEquityPropsHaveNum(equityPropsHaveNum);

        //免费领取逻辑
        if (digitalCollectionProductInfo.getCommodityType() == CommodityType.LOTTERY) {
            if (ObjectUtil.isNull(user)) {
                productDetailsVO.setReceived(false);
                productDetailsVO.setUpperLimit(0);
                productDetailsVO.setCanReceive(false);

            } else {
                productDetailsVO.setReceived(receiveProductRecordDao.getReceivedCount(productId, user.getId()) >= digitalCollectionProductInfo.getUpperLimit());
                productDetailsVO.setUpperLimit(productDetailsVO.getUpperLimit() - receiveProductRecordDao.getReceivedCount(productId, user.getId()));
            }
        }

        productDetailsVO.setIsRegisterTime(digitalCollectionProductInfo.getIsRegisterTime());
        productDetailsVO.setRegisterBeginTime(digitalCollectionProductInfo.getRegisterBeginTime());
        productDetailsVO.setRegisterEndTime(digitalCollectionProductInfo.getRegisterEndTime());
        if (digitalCollectionProductInfo.getIsRegisterTime()) {
            if (user.getCreateTime().after(digitalCollectionProductInfo.getRegisterBeginTime()) && user.getCreateTime().before(digitalCollectionProductInfo.getRegisterEndTime())) {
                productDetailsVO.setIsConforRegisterTime(Boolean.TRUE);
            }
        }
        return productDetailsVO;
    }


    private SubscribeProductDetailsVO getDefaultSubscribeProductDetails(Long productId, String appStoreVersion) {
        UserInfo user = UserPool.getUser();
        int isIosShow = 0;
        // 如果不为空,则判定账号是否为下方对应的之一.如果是的话就显示ios的
        if (ObjectUtil.isNotEmpty(user)) {
            if (user.getUserTel().equals("13849904109")) {
                isIosShow = 1;
            }
        }
        // 如果app的版本不为空的话,查找ios的版本,如果版本和ios当前版本一致,也显示ios的
        if (StrUtil.isNotBlank(appStoreVersion)) {
            AppVersion ios = appVersionService.findOneByType("ios");
            if (ios.getAppStoreVersion().equals(appStoreVersion)) {
                isIosShow = 1;
            }
        }
        // 获取指定id上架中的数字藏品的普通商品,商品有可能是ios审核商品
        DigitalCollectionProductInfo digitalCollectionProductInfo = digitalCollectionProductInfoDao.getSubscribeProductDetails(productId, isIosShow);
        if (ObjectUtil.isEmpty(digitalCollectionProductInfo)) {
            throw new GlobalRunTimeException(ErrorCode.PRODUCT_NOT_FOUND);
        }
        if (digitalCollectionProductInfo.getCommodityType() != CommodityType.SUBSCRIBE_COMMODITY) {
            throw new GlobalRunTimeException("只支持申购商品");
        }
        // 创作者Id
        Long userCreatorId = digitalCollectionProductInfo.getUserCreatorId();
        // 根据创作者Id获取创作者信息
        UserCreatorInfo userCreatorInfo = userCreatorInfoDao.getById(userCreatorId);
        if (ObjectUtil.isEmpty(userCreatorInfo)) {
            throw new GlobalRunTimeException(ErrorCode.PRODUCT_HAS_BEEN_DESCONTINUED);
        }
        // 藏品/盲盒的数量的数据
        ProductStock productStock = productStockDao.getProductStock(productId, digitalCollectionProductInfo.getRelationId(), null);
        if (ObjectUtil.isEmpty(productStock)) {
            throw new GlobalRunTimeException(ErrorCode.PRODUCT_HAS_BEEN_DESCONTINUED);
        }
        Integer sumCount = productStock.getSumCount();
        Integer usableCount = productStock.getUsableCount();
        //获取权益介绍
        StaticData staticData = staticDataDao.getByType("purchase_notes");
        // 参数复制到返回值中
        SubscribeProductDetailsVO productDetailsVO = BeanUtil.toBean(digitalCollectionProductInfo, SubscribeProductDetailsVO.class);
        Date beginTime = digitalCollectionProductInfo.getBeginTime();
        Date endTime = digitalCollectionProductInfo.getEndTime();
        // 如果权益为空的话,那么返回给前段要为"" 不可以为null  否则前段会炸
        if (ObjectUtil.isEmpty(productDetailsVO.getPrivilegePresentation())) {
            productDetailsVO.setPrivilegePresentation("");
        }
        if (ObjectUtil.isEmpty(beginTime)) {
            productDetailsVO.setBeginTime("");
        } else {
            productDetailsVO.setBeginTime(DateUtil.format(beginTime, "MM-dd HH:mm"));
        }
        if (ObjectUtil.isEmpty(endTime)) {
            productDetailsVO.setEndTime("");
        } else {
            productDetailsVO.setEndTime(DateUtil.format(endTime, "MM-dd HH:mm"));
        }

        // 数量小于等于0的时候,告知售罄
        if (usableCount <= 0) {
            productDetailsVO.setIsSoldOut(Boolean.TRUE);
            //商品状态: 1:立即购买 2:立即抢购 3:已售罄啦 4:暂未开始
            productDetailsVO.setProductType(3);
        }

        //已卖出数量
        productDetailsVO.setAlreadyNum(sumCount - usableCount);
        productDetailsVO.setLaveNum(usableCount);
        //总发售数量
//        productDetailsVO.setProductCount(sumCount);
        //限量 = 藏品总库存
        if (digitalCollectionProductInfo.getTypeClassify() == ClassifyType.COLLECTION) {
            DigitalCollection digitalCollection = digitalCollectionDao.getById(digitalCollectionProductInfo.getRelationId());
            productDetailsVO.setProductCount(digitalCollection.getSumCount());
            productDetailsVO.setModelImg(digitalCollection.getModelImg());
            productDetailsVO.setSkipUrl(digitalCollection.getSkipUrl());
            productDetailsVO.setModelDistance(digitalCollection.getModelDistance());
        } else {
            productDetailsVO.setProductCount(productStock.getSumCount());
            productDetailsVO.setModelImg(digitalCollectionProductInfo.getModelImg());
            productDetailsVO.setSkipUrl(digitalCollectionProductInfo.getSkipUrl());
            productDetailsVO.setModelDistance(digitalCollectionProductInfo.getModelDistance());
        }

        productDetailsVO.setNickName(userCreatorInfo.getDaoName());

        productDetailsVO.setCreatorPresentation(userCreatorInfo.getCreatorPresentation());
        productDetailsVO.setCreatorAvatarImg(userCreatorInfo.getCreatorAvatarImg());
        productDetailsVO.setProductId(productId);
        productDetailsVO.setPurchaseNotes(staticData.getValue());
        productDetailsVO.setFirstPay(Boolean.FALSE);
        productDetailsVO.setFirstTime(0);
        productDetailsVO.setThreeModelId(digitalCollectionProductInfo.getThreeModelId());
        productDetailsVO.setRushBuyNum(0);
        productDetailsVO.setIsRushBuy(Boolean.FALSE);
        productDetailsVO.setUpperLimit(digitalCollectionProductInfo.getUpperLimit());
        DateTime now = DateTime.now();
        //App应该显示折扣后的价格
        BigDecimal minDiscount = new BigDecimal("1");
        if (ObjectUtil.isNotEmpty(user)) {
            minDiscount = getMinDiscount(user.getId(), productId);
        }
        BigDecimal productNum = digitalCollectionProductInfo.getProductNum();
        BigDecimal bigDecimal = productNum.multiply(minDiscount).setScale(2, BigDecimal.ROUND_DOWN);
        productDetailsVO.setProductNum(bigDecimal.toPlainString());
        String equityPropsName = "";
        Integer equityPropsNum = 0;
        Integer equityPropsHaveNum = 0;
        //最大申购码数量
        Integer maxSubscribeNum = 0;
        //所需凭证数量
        Integer voucherSum = 0;
        //兑换的次数
        Integer subscribeSum = 0;
        //最大的中签数量
        Integer maxSubscribe = 0;
        //是否满足参与条件
        Boolean isMatchSubscribeCondition = true;
        // 已兑换的次数
        Integer redeemedQuantity = 0;
        //申购活动状态
        SubscribeStatusEnum subscribeStatus = SubscribeStatusEnum.NOT_START;
        //最大免费申购数
        Integer maxFreeSubscribeNum = 0;
        //最终最大申购数量
        Integer actualMaxSubscribeNum = 0;
        //规则展示图
        String ruleImg = "";
        //region 申购商品逻辑，替换原商品逻辑
        DigitalCollectionProductInfoSubscribe digitalCollectionProductInfoSubscribe = digitalCollectionProductInfoSubscribeDao.getByProductInfoSubscribe(digitalCollectionProductInfo.getId());
        if (Objects.nonNull(digitalCollectionProductInfoSubscribe)) {
            //规则展示图
            ruleImg = digitalCollectionProductInfoSubscribe.getRuleImg();
            //region 权益道具
            if (Objects.nonNull(digitalCollectionProductInfoSubscribe.getPropId())
                    && digitalCollectionProductInfoSubscribe.getPropId() > 0) {
                //权益道具数量和记录
                equityPropsName = equityPropsService.getById(digitalCollectionProductInfoSubscribe.getPropId()).getFirstTitle();
                equityPropsNum = digitalCollectionProductInfoSubscribe.getVoucherSum();
                if (ObjectUtil.isNotNull(user)) {
                    equityPropsHaveNum = userEquityPropsService.getHavingCountByUserId(digitalCollectionProductInfoSubscribe.getPropId(), user.getId());
                } else {
                    equityPropsHaveNum = 0;
                }
            }
            //endregion
            //region 申购活动配置
            productDetailsVO.setSubscribeStartTime(DateUtil.format(digitalCollectionProductInfoSubscribe.getSubscribeStartTime(), "MM-dd HH:mm"));
            productDetailsVO.setSubscribeEndTime(DateUtil.format(digitalCollectionProductInfoSubscribe.getSubscribeEndTime(), "MM-dd HH:mm"));
            productDetailsVO.setConsistenceStartTime(DateUtil.format(digitalCollectionProductInfoSubscribe.getConsistenceStartTime(), "MM-dd HH:mm"));
            productDetailsVO.setConsistenceEndTime(DateUtil.format(digitalCollectionProductInfoSubscribe.getConsistenceEndTime(), "MM-dd HH:mm"));
            maxSubscribeNum = digitalCollectionProductInfoSubscribe.getMaxSubscribeNum();
            voucherSum = digitalCollectionProductInfoSubscribe.getVoucherSum();
            subscribeSum = digitalCollectionProductInfoSubscribe.getSubscribeSum();
            maxSubscribe = digitalCollectionProductInfoSubscribe.getMaxSubscribe();
            //endregion
            //            maxSubscribeNum = maxSubscribeNum >= redeemedQuantity ? maxSubscribeNum - redeemedQuantity : 0;
            //region 申购活动状态
            if (now.isAfter(digitalCollectionProductInfoSubscribe.getSubscribeStartTime())) {
                subscribeStatus = SubscribeStatusEnum.START_SUBSCRIBE;
                if (!now.isBefore(digitalCollectionProductInfoSubscribe.getSubscribeEndTime())
                        && now.isBefore(digitalCollectionProductInfoSubscribe.getConsistenceStartTime())) {
                    subscribeStatus = SubscribeStatusEnum.END_SUBSCRIBE;
                }
                if (!now.isBefore(digitalCollectionProductInfoSubscribe.getConsistenceStartTime())
                        && now.isBefore(digitalCollectionProductInfoSubscribe.getConsistenceEndTime())) {
                    subscribeStatus = SubscribeStatusEnum.START_CONSISTENCE;
                }
                if (!now.isBefore(digitalCollectionProductInfoSubscribe.getConsistenceEndTime())
                        && now.isBefore(digitalCollectionProductInfo.getBeginTime())) {
                    subscribeStatus = SubscribeStatusEnum.RESULT_ANNOUNCE;
                }
                if (!now.isBefore(digitalCollectionProductInfo.getBeginTime())
                        && now.isBefore(digitalCollectionProductInfo.getEndTime())) {
                    subscribeStatus = SubscribeStatusEnum.START_BUY;
                }
                if (!now.isBefore(digitalCollectionProductInfo.getEndTime())) {
                    subscribeStatus = SubscribeStatusEnum.END_ACTIVITY;
                }
            }
            //endregion
            //region 是否需要参与条件
            if (digitalCollectionProductInfoSubscribe.getParticipationCondition()) {
                //region 申购条件配置
                List<DigitalCollectionProductInfoCondition> digitalCollectionProductInfoConditionList = digitalCollectionProductInfoConditionDao.getDigitalCollectionProductInfoConditionBatchByProductId(digitalCollectionProductInfo.getId());
                //endregion
                if (Objects.nonNull(digitalCollectionProductInfoConditionList) && digitalCollectionProductInfoConditionList.size() > 0) {
                    List<Long> collectionIdList = digitalCollectionProductInfoConditionList.stream().filter(condition -> Objects.nonNull(condition.getCollectionId())).map(DigitalCollectionProductInfoCondition::getCollectionId).distinct().collect(Collectors.toList());
                    List<MyCollectionCountVO> userCollectionList = (Objects.nonNull(collectionIdList) && collectionIdList.size() > 0) ? userCollectionDao.getByUserAndIdList(user.getId(), collectionIdList) : Lists.newArrayList();
                    Map<Long, MyCollectionCountVO> userCollectionMap = (Objects.nonNull(userCollectionList) && userCollectionList.size() > 0)
                            ? userCollectionList.stream().collect(Collectors.toMap(MyCollectionCountVO::getCollectionId, Function.identity()))
                            : Maps.newHashMap();
                    if (digitalCollectionProductInfoSubscribe.getConditionType() == ProductInfoSubscribeEnum.ALL) {
                        if (digitalCollectionProductInfoConditionList.stream().anyMatch(condition -> Objects.isNull(userCollectionMap.get(condition.getCollectionId())))) {
                            isMatchSubscribeCondition = false;
                        }
                    } else if (digitalCollectionProductInfoSubscribe.getConditionType() == ProductInfoSubscribeEnum.OR) {
                        if (!digitalCollectionProductInfoConditionList.stream().anyMatch(contion -> Objects.nonNull(userCollectionMap.get(contion.getCollectionId())))) {
                            isMatchSubscribeCondition = false;
                        }
                    }
                }
            }
            //endregion
            //region 可申购数及免费申购数
            //可兑换数量
//            Integer callSubscribeNum = equityPropsHaveNum / voucherSum;
            //免费可兑换数量
            if (digitalCollectionProductInfoSubscribe.getFreeSubscribe()) {
                List<DigitalCollectionProductInfoFreeSubscribe> digitalCollectionProductInfoFreeSubscribes = digitalCollectionProductInfoFreeSubscribeDao.getDigitalCollectionProductInfoFreeSubscribeAdmin(digitalCollectionProductInfo.getId());
                if (Objects.nonNull(digitalCollectionProductInfoFreeSubscribes) && digitalCollectionProductInfoFreeSubscribes.size() > 0) {
                    List<Long> collectionIdList = digitalCollectionProductInfoFreeSubscribes.stream()
                            .filter(collection -> Objects.nonNull(collection.getCollectionId()))
                            .map(DigitalCollectionProductInfoFreeSubscribe::getCollectionId)
                            .distinct()
                            .collect(Collectors.toList());
                    List<MyCollectionCountVO> userCollectionList = (Objects.nonNull(collectionIdList) && collectionIdList.size() > 0) ? userCollectionDao.getByUserAndIdList(user.getId(), collectionIdList) : Lists.newArrayList();
                    Map<Long, MyCollectionCountVO> userCollectionMap = (Objects.nonNull(userCollectionList) && userCollectionList.size() > 0)
                            ? userCollectionList.stream().collect(Collectors.toMap(MyCollectionCountVO::getCollectionId, Function.identity()))
                            : Maps.newHashMap();
                    for (DigitalCollectionProductInfoFreeSubscribe digitalCollectionProductInfoFreeSubscribe : digitalCollectionProductInfoFreeSubscribes) {
                        if (Objects.isNull(digitalCollectionProductInfoFreeSubscribe.getCollectionId())
                                || digitalCollectionProductInfoFreeSubscribe.getCollectionId() <= 0) {
                            maxFreeSubscribeNum = maxFreeSubscribeNum + digitalCollectionProductInfoFreeSubscribe.getQuantity();
                        } else if (Objects.nonNull(userCollectionMap.get(digitalCollectionProductInfoFreeSubscribe.getCollectionId()))) {
                            maxFreeSubscribeNum = maxFreeSubscribeNum + digitalCollectionProductInfoFreeSubscribe.getQuantity() * (userCollectionMap.get(digitalCollectionProductInfoFreeSubscribe.getCollectionId()).getCount());
                        }
                    }
                }
            }
            // 获取已经兑换的免费申购码的数量.把已经兑换的免费的减去maxFreeSubscribeNum
            Integer countByUserProductIdAndSourece = digitalCollectionProductInfoSubscribeCodeDao.getCountByUserProductIdAndSourece(user.getId(), productId, ProductInfoSubscribeSourceEnum.FREE);
            // 减去已经领取的免费申购的,才是剩余可领取的
            maxFreeSubscribeNum = maxFreeSubscribeNum - countByUserProductIdAndSourece;
//            actualMaxSubscribeNum = NumberUtil.min(maxSubscribeNum, callSubscribeNum + maxFreeSubscribeNum);
            //已兑换数量
            redeemedQuantity = digitalCollectionProductInfoSubscribeCodeDao.getCountByUserProductIdAndSchedule(user.getId(), productId, null);
            //实际最大可兑换数 = Math.min(配置最大可兑换数 - 已兑换数, 拥有道具数/所需凭证数量) + 最大免费可兑换数量
            actualMaxSubscribeNum = Math.min(maxSubscribeNum - redeemedQuantity, equityPropsHaveNum/voucherSum) + maxFreeSubscribeNum;
            //endregion
        }
        //endregion
        productDetailsVO.setEquityPropsName(equityPropsName);
        productDetailsVO.setEquityPropsNum(equityPropsNum);
        //查询用户持有的数量
        productDetailsVO.setEquityPropsHaveNum(equityPropsHaveNum);
        productDetailsVO.setMaxSubscribeNum(maxSubscribeNum);
        productDetailsVO.setVoucherSum(voucherSum);
        productDetailsVO.setSubscribeSum(subscribeSum);
        productDetailsVO.setMaxSubscribe(maxSubscribe);
        productDetailsVO.setIsMatchSubscribeCondition(isMatchSubscribeCondition);
        productDetailsVO.setSubscribeStatus(subscribeStatus);
        productDetailsVO.setMaxFreeSubscribeNum(maxFreeSubscribeNum > 0 ? maxFreeSubscribeNum : 0);
        productDetailsVO.setActualMaxSubscribeNum(actualMaxSubscribeNum);
        productDetailsVO.setRuleImg(ruleImg);
        productDetailsVO.setRedeemedQuantity(redeemedQuantity);
        //免费领取逻辑
        if (digitalCollectionProductInfo.getCommodityType() == CommodityType.LOTTERY) {
            if (ObjectUtil.isNull(user)) {
                productDetailsVO.setReceived(false);
                productDetailsVO.setUpperLimit(0);
                productDetailsVO.setCanReceive(false);

            } else {
                productDetailsVO.setReceived(receiveProductRecordDao.getReceivedCount(productId, user.getId()) >= digitalCollectionProductInfo.getUpperLimit());
                productDetailsVO.setUpperLimit(productDetailsVO.getUpperLimit() - receiveProductRecordDao.getReceivedCount(productId, user.getId()));
            }
        }

        productDetailsVO.setIsRegisterTime(digitalCollectionProductInfo.getIsRegisterTime());
        productDetailsVO.setRegisterBeginTime(digitalCollectionProductInfo.getRegisterBeginTime());
        productDetailsVO.setRegisterEndTime(digitalCollectionProductInfo.getRegisterEndTime());
        if (digitalCollectionProductInfo.getIsRegisterTime()) {
            if (user.getCreateTime().after(digitalCollectionProductInfo.getRegisterBeginTime()) && user.getCreateTime().before(digitalCollectionProductInfo.getRegisterEndTime())) {
                productDetailsVO.setIsConforRegisterTime(Boolean.TRUE);
            }
        }
        //region 是否中签
        Boolean isWin = false;
        Integer productIdAndUserIdByCount = digitalCollectionProductInfoSubscribeWinBallotDao.getByProductIdAndUserIdAndWinStatus(productId, user.getId(), null);
        if (productIdAndUserIdByCount > 0 && now.isAfter(digitalCollectionProductInfoSubscribe.getConsistenceEndTime())) {
            isWin = true;
        }
        // 中签数量
        productDetailsVO.setCallUseCount(digitalCollectionProductInfoSubscribeWinBallotDao.getProductIdAndUserIdByCount(productId, user.getId()));

        productDetailsVO.setIsWin(isWin);
        //endregion
        return productDetailsVO;
    }

    private ProductDetailsVO getPrivateSalvageProductDetails(Long productId, String appStoreVersion) {
        UserInfo user = UserPool.getUser();
        int isIosShow = 0;
        // 如果不为空,则判定账号是否为下方对应的之一.如果是的话就显示ios的
        if (ObjectUtil.isNotEmpty(user)) {
            if (user.getUserTel().equals("13849904109")) {
                isIosShow = 1;
            }
        }
        // 如果app的版本不为空的话,查找ios的版本,如果版本和ios当前版本一致,也显示ios的
        if (StrUtil.isNotBlank(appStoreVersion)) {
            AppVersion ios = appVersionService.findOneByType("ios");
            if (ios.getAppStoreVersion().equals(appStoreVersion)) {
                isIosShow = 1;
            }
        }
        // 获取指定id上架中的数字藏品的普通商品,商品有可能是ios审核商品
        DigitalCollectionProductInfo digitalCollectionProductInfo = digitalCollectionProductInfoDao.getOneByProductIdAndCommodityType(productId, CommodityType.SALVAGE_COLLECTION);
        if (ObjectUtil.isEmpty(digitalCollectionProductInfo)) {
            throw new GlobalRunTimeException(ErrorCode.PRODUCT_NOT_FOUND);
        }
        // 创作者Id
        Long userCreatorId = digitalCollectionProductInfo.getUserCreatorId();
        // 根据创作者Id获取创作者信息
        UserCreatorInfo userCreatorInfo = userCreatorInfoDao.getById(userCreatorId);
        if (ObjectUtil.isEmpty(userCreatorInfo)) {
            throw new GlobalRunTimeException(ErrorCode.PRODUCT_HAS_BEEN_DESCONTINUED);
        }
        // 藏品/盲盒的数量的数据
        ProductStock productStock = productStockDao.getProductStock(productId, digitalCollectionProductInfo.getRelationId(), null);
        if (ObjectUtil.isEmpty(productStock)) {
            throw new GlobalRunTimeException(ErrorCode.PRODUCT_HAS_BEEN_DESCONTINUED);
        }
        Integer sumCount = productStock.getSumCount();
        Integer usableCount = productStock.getUsableCount();
        //获取权益介绍
        StaticData staticData = staticDataDao.getByType("purchase_notes");
        // 参数复制到返回值中
        ProductDetailsVO productDetailsVO = BeanUtil.toBean(digitalCollectionProductInfo, ProductDetailsVO.class);
        Date beginTime = digitalCollectionProductInfo.getBeginTime();
        // 如果权益为空的话,那么返回给前段要为"" 不可以为null  否则前段会炸
        if (ObjectUtil.isEmpty(productDetailsVO.getPrivilegePresentation())) {
            productDetailsVO.setPrivilegePresentation("");
        }

        // 如果抢购时间为null的话,也要返回""否则的话按照MM-dd HH:mm:ss进行返回
        if (ObjectUtil.isEmpty(beginTime)) {
            productDetailsVO.setBeginTime("");
        } else {
            String forDate = DateUtil.format(beginTime, "MM-dd HH:mm:ss");
            productDetailsVO.setBeginTime(forDate);
        }

        // 数量小于等于0的时候,告知售罄
        if (usableCount <= 0) {
            productDetailsVO.setIsSoldOut(Boolean.TRUE);
            //商品状态: 1:立即购买 2:立即抢购 3:已售罄啦 4:暂未开始
            productDetailsVO.setProductType(3);
        }

        //已卖出数量
        productDetailsVO.setAlreadyNum(sumCount - usableCount);
        productDetailsVO.setLaveNum(usableCount);
        //总发售数量
//        productDetailsVO.setProductCount(sumCount);
        //限量 = 藏品总库存
        if (digitalCollectionProductInfo.getTypeClassify() == ClassifyType.COLLECTION) {
            DigitalCollection digitalCollection = digitalCollectionDao.getById(digitalCollectionProductInfo.getRelationId());
            productDetailsVO.setProductCount(digitalCollection.getSumCount());
            productDetailsVO.setModelImg(digitalCollection.getModelImg());
            productDetailsVO.setSkipUrl(digitalCollection.getSkipUrl());
            productDetailsVO.setModelDistance(digitalCollection.getModelDistance());
        } else {
            productDetailsVO.setProductCount(productStock.getSumCount());
            productDetailsVO.setModelImg(digitalCollectionProductInfo.getModelImg());
            productDetailsVO.setSkipUrl(digitalCollectionProductInfo.getSkipUrl());
            productDetailsVO.setModelDistance(digitalCollectionProductInfo.getModelDistance());
        }

        productDetailsVO.setNickName(userCreatorInfo.getDaoName());

        productDetailsVO.setCreatorPresentation(userCreatorInfo.getCreatorPresentation());
        productDetailsVO.setCreatorAvatarImg(userCreatorInfo.getCreatorAvatarImg());
        productDetailsVO.setProductId(productId);
        productDetailsVO.setPurchaseNotes(staticData.getValue());
        productDetailsVO.setFirstPay(Boolean.FALSE);
        productDetailsVO.setFirstTime(0);
        productDetailsVO.setThreeModelId(digitalCollectionProductInfo.getThreeModelId());
        productDetailsVO.setUpperLimit(digitalCollectionProductInfo.getUpperLimit());
        Integer productType = productDetailsVO.getProductType();
        productDetailsVO.setRushBuyNum(0);
        productDetailsVO.setIsRushBuy(Boolean.FALSE);

        //用户可以持有的上限
        Integer userHoldCount = userCollectionDao.getHoldCountByCollectionId(digitalCollectionProductInfo.getRelationId(), user.getId());
        productDetailsVO.setUpperLimit(digitalCollectionProductInfo.getUpperLimit() - userHoldCount < 0 ? 0 : digitalCollectionProductInfo.getUpperLimit() - userHoldCount);
        DateTime now = DateTime.now();
        UserInfo loginUser = UserPool.getUser();
        List<ProductFirstDTO> productFirstDTOS = Lists.newArrayList();
        List<RushBuyDTO> rushBuyList = Lists.newArrayList();
        //优先购
        productFirstDTOS = getBuyFirstList(digitalCollectionProductInfo, loginUser, now);
        if (productFirstDTOS.size() > 0) {
            int sumFirstPayTime = productFirstDTOS.stream().mapToInt(x -> x.getSumCount() - x.getUseCount()).sum();
            productDetailsVO.setFirstTime(Math.min(sumFirstPayTime, digitalCollectionProductInfo.getPriorityPurchaseLimit()));
            if (productDetailsVO.getFirstTime() > 0) {
                productDetailsVO.setFirstPay(Boolean.TRUE);
            }
            productDetailsVO.setFirstDTOS(productFirstDTOS.stream().sorted(Comparator.comparing(ProductFirstDTO::getType)).collect(Collectors.toList()));
            productDetailsVO.setRushBuyDTOS(rushBuyList);
        } else {
            productDetailsVO.setFirstDTOS(productFirstDTOS);


            //优先抢逻辑
            rushBuyList = getRushBuyList(digitalCollectionProductInfo, loginUser, now);
            if (rushBuyList.size() > 0) {
                // 购买上限数量
                int rushBuyNum = digitalCollectionProductInfo.getUpperLimit();
                productDetailsVO.setRushBuyNum(rushBuyNum);
                if (productDetailsVO.getRushBuyNum() > 0) {
                    productDetailsVO.setIsRushBuy(Boolean.TRUE);
                }
                productDetailsVO.setRushBuyDTOS(rushBuyList.stream().sorted(Comparator.comparing(RushBuyDTO::getType)).collect(Collectors.toList()));
            } else {
                productDetailsVO.setRushBuyDTOS(rushBuyList);
            }
        }
        //App应该显示折扣后的价格
        BigDecimal minDiscount = new BigDecimal("1");
        if (ObjectUtil.isNotEmpty(user)) {
            minDiscount = getMinDiscount(user.getId(), productId);
        }
        BigDecimal productNum = digitalCollectionProductInfo.getProductNum();
        BigDecimal bigDecimal = productNum.multiply(minDiscount).setScale(2, BigDecimal.ROUND_DOWN);
        productDetailsVO.setProductNum(bigDecimal.toPlainString());
        String equityPropsName = "";
        Integer equityPropsNum = 0;
        Integer equityPropsHaveNum = 0;
        Long equityPropsId = digitalCollectionProductInfo.getEquityPropsId();
        if (ObjectUtil.isNotNull(equityPropsId) && equityPropsId.intValue() > 0) {
            //权益道具数量和记录
            equityPropsName = equityPropsService.getById(digitalCollectionProductInfo.getEquityPropsId()).getFirstTitle();
            equityPropsNum = digitalCollectionProductInfo.getEquityPropsNum();
            if (ObjectUtil.isNotNull(user)) {
                equityPropsHaveNum = userEquityPropsService.getHavingCountByUserId(digitalCollectionProductInfo.getEquityPropsId(), user.getId());
            } else {
                equityPropsHaveNum = 0;
            }
        }
        productDetailsVO.setEquityPropsName(equityPropsName);
        productDetailsVO.setEquityPropsNum(equityPropsNum);
        //查询用户持有的数量
        productDetailsVO.setEquityPropsHaveNum(equityPropsHaveNum);
        return productDetailsVO;
    }

    /**
     * 获取最高折扣
     *
     * @param userId
     * @param productId
     * @return
     */
    private BigDecimal getMinDiscount(Long userId, Long productId) {
        //折扣集合
        ArrayList<BigDecimal> collectionDiscount = Lists.newArrayList();
        //最大的折扣
        BigDecimal minDiscount = new BigDecimal("1");

        //设置的藏品关联
        List<BuyCollectionDiscount> collectionList = buyCollectionDiscountDao.getMinDiscountByProductId(productId);
        if (collectionList.size() > 0) {
            for (BuyCollectionDiscount buyCollectionDiscount : collectionList) {
                Long collectionBuyFirstId = buyCollectionDiscount.getCollectionBuyFirstId();
                List<UserCollection> collercitonList = userCollectionDao.getListByCollercitonId(userId, collectionBuyFirstId);
                if (collercitonList.size() > 0) {
                    collectionDiscount.add(buyCollectionDiscount.getBuyFirstNum());
                }
            }
        }
        //设置的用户关联
        List<BuyUserDiscount> minDiscountByUserId = buyUserDiscountDao.getMinDiscountByUserId(userId, productId);
        if (minDiscountByUserId.size() > 0) {
            for (BuyUserDiscount buyUserDiscount : minDiscountByUserId) {
                BigDecimal buyFirstNum = buyUserDiscount.getBuyFirstNum();
                collectionDiscount.add(buyFirstNum);
            }
        }

        if (collectionDiscount.size() > 0) {
            minDiscount = Collections.min(collectionDiscount);
        }

        return minDiscount;
    }

    /**
     * 优先抢
     * <p>
     * 1.判断是否是优先抢的时间段
     * 2.判断优先抢的剩余数量(上限数量 减去 已出售数量)
     * 3.判断自己是否达到藏品的上限
     *
     * @param digitalCollectionProductInfo
     * @param loginUser
     * @param now
     */
    private List<RushBuyDTO> getRushBuyList(DigitalCollectionProductInfo digitalCollectionProductInfo, UserInfo loginUser, DateTime now) {
        Boolean isRushBuy = digitalCollectionProductInfo.getIsRushBuy();
        Date rushBuyBeginTime = digitalCollectionProductInfo.getRushBuyBeginTime();
        Date rushBuyEndTime = digitalCollectionProductInfo.getRushBuyEndTime();
        Integer rushBuyLimit = digitalCollectionProductInfo.getRushBuyLimit();
        Integer upperLimit = digitalCollectionProductInfo.getUpperLimit();
        Long productId = digitalCollectionProductInfo.getId();
        Integer productType = digitalCollectionProductInfo.getProductType();
        List<RushBuyDTO> rushBuyDTOList = Lists.newArrayList();
        //todo 校验是否是优先抢
        if (isRushBuy && productType == 4 && rushBuyBeginTime != null && rushBuyEndTime != null && rushBuyLimit != null
                && now.isBefore(rushBuyEndTime)
                && now.isAfter(rushBuyBeginTime)
                && rushBuyLimit > 0) {

            if (ObjectUtil.isNotEmpty(loginUser)) {
                //已经优先抢的订单
                int sum = orderDao.getRushBuySumListByProductIdAndStaus(digitalCollectionProductInfo.getId());
                //当前用户已经购买的数量
                int userRushBuySum = orderDao.getRushBuySumByProductIdAndStausAndUserId(digitalCollectionProductInfo.getId(), loginUser.getId());
                //优先抢的藏品
                List<RushBuyCollection> rushBuyCollectionList = rushBuyCollectionDao.selectByProductId(productId);
                //上限数量 > 已出售数量            自己是否达到藏品的上限
                if (rushBuyLimit > sum && upperLimit > userRushBuySum) {
                    if (CollUtil.isNotEmpty(rushBuyCollectionList)) {
                        //用户藏品
                        List<UserCollection> userCollections = userCollectionDao.getUserCollection(loginUser.getId());
                        for (RushBuyCollection rushBuyCollection : rushBuyCollectionList) {
                            for (UserCollection userCollection : userCollections) {
                                if (rushBuyCollection.getCollectionBuyFirstId().longValue() == userCollection.getCollectionId().longValue()) {
                                    // 获取用户已经购买的商品的数量总数
                                    List<RushBuyRecord> rushBuyRecordList = rushBuyRecordDao.getByProductIdAndCollectionUuid(productId, userCollection.getCollectionUuid());
                                    int sumBuyCount = rushBuyRecordList.stream().mapToInt(RushBuyRecord::getBuyCount).sum();
                                    // 如果可以购买则添加进去集合中
                                    if (rushBuyCollection.getBuyFirstNum() > sumBuyCount) {
                                        RushBuyDTO rushBuyDTO = new RushBuyDTO();
                                        rushBuyDTO.setType(1);
                                        rushBuyDTO.setCollectionUuid(userCollection.getCollectionUuid());
                                        rushBuyDTO.setCollectionName(userCollection.getFirstTitle());
                                        rushBuyDTO.setSumCount(rushBuyCollection.getBuyFirstNum());
                                        rushBuyDTO.setUseCount(sumBuyCount);
                                        rushBuyDTOList.add(rushBuyDTO);
                                    }
                                }
                            }
                        }
                    }
                    // 查询用户是否有优先购买权
                    RushBuyUser rushBuyUser = rushBuyUserDao.selectByProductIdAndUserId(productId, loginUser.getId());
                    if (ObjectUtil.isNotNull(rushBuyUser)) {
                        // 查询用户已经优先购买的数量
                        List<RushBuyRecord> rushBuyRecordList = rushBuyRecordDao.getByProductIdAndUserId(productId, loginUser.getId());
                        Integer sumOrderBuyCount = rushBuyRecordList.stream().mapToInt(RushBuyRecord::getBuyCount).sum();
                        if (rushBuyUser.getBuyFirstNum() > sumOrderBuyCount) {
                            RushBuyDTO rushBuyDTO = new RushBuyDTO();
                            rushBuyDTO.setType(2);
                            rushBuyDTO.setSumCount(rushBuyUser.getBuyFirstNum());
                            rushBuyDTO.setUseCount(sumOrderBuyCount);
                            rushBuyDTO.setCollectionName("白名单");
                            rushBuyDTO.setCollectionUuid("");
                            rushBuyDTOList.add(rushBuyDTO);
                        }
                    }
                }
            }
        }
        return rushBuyDTOList;
    }


    /**
     * 优先购
     *
     * @param digitalCollectionProductInfo
     * @param loginUser
     * @param now
     */
    private List<ProductFirstDTO> getBuyFirstList(DigitalCollectionProductInfo digitalCollectionProductInfo, UserInfo loginUser, DateTime now) {
        // 是否是优先购商品 1:是 0不是
        Boolean isPriorityPurchase = digitalCollectionProductInfo.getIsPriorityPurchase();
        // 优先购开始时间
        Date priorityPurchaseBeginTime = digitalCollectionProductInfo.getPriorityPurchaseBeginTime();
        // 优先购结束时间
        Date priorityPurchaseEndTime = digitalCollectionProductInfo.getPriorityPurchaseEndTime();
        // 商品状态: 1:立即购买 2:立即抢购 3:已售罄啦 4:暂未开始
        Integer productType = digitalCollectionProductInfo.getProductType();
        // 商品Id
        Long productId = digitalCollectionProductInfo.getId();
        List<ProductFirstDTO> productFirstDTOS = Lists.newArrayList();
        //  1:立即购买 2:立即抢购 3:已售罄啦 4:暂未开始
        if (isPriorityPurchase && productType == 4 && priorityPurchaseBeginTime != null && priorityPurchaseEndTime != null && digitalCollectionProductInfo.getPriorityPurchaseLimit() != null
                && now.isBefore(priorityPurchaseEndTime)
                && now.isAfter(priorityPurchaseBeginTime)
                && digitalCollectionProductInfo.getPriorityPurchaseLimit() > 0) {
            if (ObjectUtil.isNotEmpty(loginUser)) {
                // 优先购买的藏品列表
                List<BuyFirstCollection> buyFirstCollections = buyFirstCollectionDao.selectByProductId(productId);
                if (CollUtil.isNotEmpty(buyFirstCollections)) {
                    // 用户藏品列表
                    List<UserCollection> userCollections = userCollectionDao.getUserCollection(loginUser.getId());
                    // 循环比对
                    for (BuyFirstCollection buyFirstCollection : buyFirstCollections) {
                        for (UserCollection userCollection : userCollections) {
                            // 如果优先购的藏品和用户的藏品一致的话,做后续的操作
                            if (buyFirstCollection.getCollectionBuyFirstId().longValue() == userCollection.getCollectionId().longValue()) {
                                // 根据权益查询用户的优先购次数
                                Privilege privilege = privilegeDao.getByCollectionId(userCollection.getCollectionId());
                                //特权卡
                                if (ObjectUtil.isNotNull(privilege)) {
                                    String yearMonth = now.toString("yyyyMM");
                                    //当月使用次数
                                    int privileCount = privilegeUseLogDao.getFirstCountByRecordMonth(yearMonth, userCollection.getCollectionUuid());
                                    //特权卡数量 大于 当月使用数量
                                    if (privilege.getFirstPayTime() >= privileCount) {
                                        //如果最后一次购买的不是这个商品 就没有必要展示
                                        FirstBuyRecord firstBuyRecord = firstBuyRecordDao.getByCollectionUuid(userCollection.getCollectionUuid());
                                        if (ObjectUtil.isNotEmpty(firstBuyRecord) && privilege.getFirstPayTime() == privileCount && firstBuyRecord.getProductId().longValue() != productId) {
                                            continue;
                                        }
                                        //某个特权卡优先购的记录
                                        List<FirstBuyRecord> firstBuyRecordList = firstBuyRecordDao.getByProductIdAndCollectionUuid(productId, userCollection.getCollectionUuid());
                                        // 获取记录总数
                                        int sumBuyCount = firstBuyRecordList.stream().mapToInt(FirstBuyRecord::getBuyCount).sum();
                                        // 如果总数小于优先购数量则返回
                                        if (buyFirstCollection.getBuyFirstNum() > sumBuyCount) {
                                            ProductFirstDTO productFirstDTO = new ProductFirstDTO();
                                            productFirstDTO.setType(0);
                                            productFirstDTO.setCollectionUuid(userCollection.getCollectionUuid());
                                            productFirstDTO.setCollectionName(userCollection.getFirstTitle());
                                            productFirstDTO.setSumCount(buyFirstCollection.getBuyFirstNum());
                                            productFirstDTO.setUseCount(sumBuyCount);
                                            productFirstDTOS.add(productFirstDTO);
                                        }
                                    }
                                } else {
                                    // 特权卡为空的操作
                                    List<FirstBuyRecord> firstBuyRecordList = firstBuyRecordDao.getByProductIdAndCollectionUuid(productId, userCollection.getCollectionUuid());
                                    int sumBuyCount = firstBuyRecordList.stream().mapToInt(FirstBuyRecord::getBuyCount).sum();
                                    if (buyFirstCollection.getBuyFirstNum() - sumBuyCount > 0) {
                                        ProductFirstDTO productFirstDTO = new ProductFirstDTO();
                                        productFirstDTO.setType(1);
                                        productFirstDTO.setCollectionUuid(userCollection.getCollectionUuid());
                                        productFirstDTO.setCollectionName(userCollection.getFirstTitle());
                                        productFirstDTO.setSumCount(buyFirstCollection.getBuyFirstNum());
                                        productFirstDTO.setUseCount(sumBuyCount);
                                        productFirstDTOS.add(productFirstDTO);
                                    }
                                }
                            }
                        }
                    }
                }

                // 优先购的用户和数量
                BuyFirstUser buyFirstUser = buyFirstUserDao.selectByProductIdAndUserId(productId, loginUser.getId());
                if (ObjectUtil.isNotNull(buyFirstUser)) {
                    List<FirstBuyRecord> firstBuyRecordList = firstBuyRecordDao.getByProductIdAndUserId(productId, loginUser.getId());
                    Integer sumOrderBuyCount = firstBuyRecordList.stream().mapToInt(FirstBuyRecord::getBuyCount).sum();
                    if (buyFirstUser.getBuyFirstNum() > sumOrderBuyCount) {
                        ProductFirstDTO productFirstDTO = new ProductFirstDTO();
                        productFirstDTO.setType(2);
                        productFirstDTO.setSumCount(buyFirstUser.getBuyFirstNum());
                        productFirstDTO.setUseCount(sumOrderBuyCount);
                        productFirstDTO.setCollectionName("白名单");
                        productFirstDTO.setCollectionUuid("");
                        productFirstDTOS.add(productFirstDTO);
                    }
                }
            }
        }
        return productFirstDTOS;
    }

    private AuctionProductDetailsVO getDefaultAuctionProductDetails(Long productId, String appStoreVersion) {
        UserInfo user = UserPool.getUser();
        int isIosShow = 0;
        // 如果不为空,则判定账号是否为下方对应的之一.如果是的话就显示ios的
        if (ObjectUtil.isNotEmpty(user)) {
            if (user.getUserTel().equals("13849904109")) {
                isIosShow = 1;
            }
        }
        // 如果app的版本不为空的话,查找ios的版本,如果版本和ios当前版本一致,也显示ios的
        if (StrUtil.isNotBlank(appStoreVersion)) {
            AppVersion ios = appVersionService.findOneByType("ios");
            if (ios.getAppStoreVersion().equals(appStoreVersion)) {
                isIosShow = 1;
            }
        }
        // 获取指定id上架中的数字藏品的普通商品,商品有可能是ios审核商品
        DigitalCollectionProductInfo digitalCollectionProductInfo = digitalCollectionProductInfoDao.getAuctionProductDetails(productId, isIosShow);
        if (ObjectUtil.isEmpty(digitalCollectionProductInfo)) {
            throw new GlobalRunTimeException(ErrorCode.PRODUCT_NOT_FOUND);
        }
        if (digitalCollectionProductInfo.getCommodityType() != CommodityType.AUCTION_PRODUCT) {
            throw new GlobalRunTimeException("只支持拍卖商品");
        }
        DigitalCollectionProductInfoAuction collectionProductInfoAuction = digitalCollectionProductInfoAuctionDao.getByProductId(productId);
        if (ObjectUtil.isEmpty(collectionProductInfoAuction)) {
            throw new GlobalRunTimeException(ErrorCode.PRODUCT_NOT_FOUND);
        }
        // 创作者Id
        Long userCreatorId = digitalCollectionProductInfo.getUserCreatorId();
        // 根据创作者Id获取创作者信息
        UserCreatorInfo userCreatorInfo = userCreatorInfoDao.getById(userCreatorId);
        if (ObjectUtil.isEmpty(userCreatorInfo)) {
            throw new GlobalRunTimeException(ErrorCode.PRODUCT_HAS_BEEN_DESCONTINUED);
        }
        // 藏品/盲盒的数量的数据
        ProductStock productStock = productStockDao.getProductStock(productId, digitalCollectionProductInfo.getRelationId(), null);
        if (ObjectUtil.isEmpty(productStock)) {
            throw new GlobalRunTimeException(ErrorCode.PRODUCT_HAS_BEEN_DESCONTINUED);
        }
        Integer sumCount = productStock.getSumCount();
        Integer usableCount = productStock.getUsableCount();
        //获取权益介绍
        StaticData staticData = staticDataDao.getByType("purchase_notes");
        AuctionProductDetailsVO productDetailsVO = BeanUtil.toBean(digitalCollectionProductInfo, AuctionProductDetailsVO.class);
        Date beginTime = collectionProductInfoAuction.getAuctionStartTime();
        Date endTime = collectionProductInfoAuction.getAuctionEndTime();
        // 如果权益为空的话,那么返回给前段要为"" 不可以为null  否则前段会炸
        if (ObjectUtil.isEmpty(productDetailsVO.getPrivilegePresentation())) {
            productDetailsVO.setPrivilegePresentation("");
        }
        if (ObjectUtil.isEmpty(beginTime)) {
            productDetailsVO.setBeginTime("");
        } else {
            productDetailsVO.setBeginTime(DateUtil.format(beginTime, "MM-dd HH:mm"));
        }
        if (ObjectUtil.isEmpty(endTime)) {
            productDetailsVO.setEndTime("");
        } else {
            productDetailsVO.setEndTime(DateUtil.format(endTime, "MM-dd HH:mm"));
        }

        // 数量小于等于0的时候,告知售罄
        if (usableCount <= 0) {
            productDetailsVO.setIsSoldOut(Boolean.TRUE);
            //商品状态: 1:立即购买 2:立即抢购 3:已售罄啦 4:暂未开始
            productDetailsVO.setProductType(3);
        }

        //已卖出数量
        productDetailsVO.setAlreadyNum(sumCount - usableCount);
        productDetailsVO.setLaveNum(usableCount);
        //总发售数量
//        productDetailsVO.setProductCount(sumCount);
        //限量 = 藏品总库存
        if (digitalCollectionProductInfo.getTypeClassify() == ClassifyType.COLLECTION) {
            DigitalCollection digitalCollection = digitalCollectionDao.getById(digitalCollectionProductInfo.getRelationId());
            productDetailsVO.setProductCount(digitalCollection.getSumCount());
            productDetailsVO.setModelImg(digitalCollection.getModelImg());
            productDetailsVO.setSkipUrl(digitalCollection.getSkipUrl());
            productDetailsVO.setModelDistance(digitalCollection.getModelDistance());
        } else {
            productDetailsVO.setProductCount(productStock.getSumCount());
            productDetailsVO.setModelImg(digitalCollectionProductInfo.getModelImg());
            productDetailsVO.setSkipUrl(digitalCollectionProductInfo.getSkipUrl());
            productDetailsVO.setModelDistance(digitalCollectionProductInfo.getModelDistance());
        }

        productDetailsVO.setNickName(userCreatorInfo.getDaoName());

        productDetailsVO.setCreatorPresentation(userCreatorInfo.getCreatorPresentation());
        productDetailsVO.setCreatorAvatarImg(userCreatorInfo.getCreatorAvatarImg());
        productDetailsVO.setProductId(productId);
        productDetailsVO.setPurchaseNotes(staticData.getValue());
        productDetailsVO.setFirstPay(Boolean.FALSE);
        productDetailsVO.setFirstTime(0);
        productDetailsVO.setThreeModelId(digitalCollectionProductInfo.getThreeModelId());
        productDetailsVO.setRushBuyNum(0);
        productDetailsVO.setIsRushBuy(Boolean.FALSE);
        productDetailsVO.setUpperLimit(digitalCollectionProductInfo.getUpperLimit());
        DateTime now = DateTime.now();
        //App应该显示折扣后的价格
        BigDecimal minDiscount = new BigDecimal("1");
        if (ObjectUtil.isNotEmpty(user)) {
            minDiscount = getMinDiscount(user.getId(), productId);
        }
        BigDecimal productNum = digitalCollectionProductInfo.getProductNum();
        BigDecimal bigDecimal = productNum.multiply(minDiscount).setScale(2, BigDecimal.ROUND_DOWN);
        productDetailsVO.setProductNum(bigDecimal.toPlainString());
        String equityPropsName = "";
        Integer equityPropsNum = 0;
        Integer equityPropsHaveNum = 0;
        Long equityPropsId = digitalCollectionProductInfo.getEquityPropsId();
        if (ObjectUtil.isNotNull(equityPropsId) && equityPropsId.intValue() > 0) {
            //权益道具数量和记录
            equityPropsName = equityPropsService.getById(digitalCollectionProductInfo.getEquityPropsId()).getFirstTitle();
            equityPropsNum = digitalCollectionProductInfo.getEquityPropsNum();
            if (ObjectUtil.isNotNull(user)) {
                equityPropsHaveNum = userEquityPropsService.getHavingCountByUserId(digitalCollectionProductInfo.getEquityPropsId(), user.getId());
            } else {
                equityPropsHaveNum = 0;
            }
        }
        productDetailsVO.setEquityPropsName(equityPropsName);
        productDetailsVO.setEquityPropsNum(equityPropsNum);
        //查询用户持有的数量
        productDetailsVO.setEquityPropsHaveNum(equityPropsHaveNum);
        //免费领取逻辑
        if (digitalCollectionProductInfo.getCommodityType() == CommodityType.LOTTERY) {
            if (ObjectUtil.isNull(user)) {
                productDetailsVO.setReceived(false);
                productDetailsVO.setUpperLimit(0);
                productDetailsVO.setCanReceive(false);

            } else {
                productDetailsVO.setReceived(receiveProductRecordDao.getReceivedCount(productId, user.getId()) >= digitalCollectionProductInfo.getUpperLimit());
                productDetailsVO.setUpperLimit(productDetailsVO.getUpperLimit() - receiveProductRecordDao.getReceivedCount(productId, user.getId()));
            }
        }

        productDetailsVO.setIsRegisterTime(digitalCollectionProductInfo.getIsRegisterTime());
        productDetailsVO.setRegisterBeginTime(digitalCollectionProductInfo.getRegisterBeginTime());
        productDetailsVO.setRegisterEndTime(digitalCollectionProductInfo.getRegisterEndTime());
        if (digitalCollectionProductInfo.getIsRegisterTime()) {
            if (user.getCreateTime().after(digitalCollectionProductInfo.getRegisterBeginTime()) && user.getCreateTime().before(digitalCollectionProductInfo.getRegisterEndTime())) {
                productDetailsVO.setIsConforRegisterTime(Boolean.TRUE);
            }
        }
        //我的质押物数量
        Integer pledgeNum = 0;
        //拍卖状态
        AuctionStatusEnum auctionStatus = AuctionStatusEnum.NOT_START;
        //是否中签
        Boolean winningBid = Boolean.FALSE;
        //region 拍卖状态
        if (now.isAfter(collectionProductInfoAuction.getAuctionStartTime())) {
            auctionStatus = AuctionStatusEnum.START_AUCTION;
            if (!now.isBefore(collectionProductInfoAuction.getAuctionEndTime())) {
                auctionStatus = AuctionStatusEnum.END_AUCTION;
            }
            List<DigitalCollectionProductInfoAuctionUserBidding> userBiddingList = digitalCollectionProductInfoAuctionUserBiddingDao.listByProductIdAndUserId(user.getId(), productId);
            if (!CollectionUtils.isEmpty(userBiddingList)) {
                pledgeNum = userBiddingList.stream().mapToInt(DigitalCollectionProductInfoAuctionUserBidding::getBiddingNum).sum();
                if (auctionStatus == AuctionStatusEnum.END_AUCTION) {
                    //查询用户是否中标
                    DigitalCollectionProductInfoAuctionUserBiddingResults productIdAndUserId = digitalCollectionProductInfoAuctionUserBiddingResultsDao.getByProductIdAndUserId(user.getId(), productId);
                    if (Objects.nonNull(productIdAndUserId)) {
                        AcutionProgressEnum progress = productIdAndUserId.getProgress();
                        winningBid = progress == AcutionProgressEnum.WINNING_BID;
                    }
                }
            }

        }
        int count = digitalCollectionProductInfoAuctionUserAgreementDao.countByProductIdAndUserId(productId, user.getId());
        if (count > 0) {
            productDetailsVO.setAgreement(Boolean.TRUE);
        }
        productDetailsVO.setPledgeNum(pledgeNum);
        productDetailsVO.setAuctionStatus(auctionStatus);
        productDetailsVO.setDrawPrize(collectionProductInfoAuction.getHaveResult());
        productDetailsVO.setWinningBid(winningBid);
        productDetailsVO.setRuleImg(collectionProductInfoAuction.getRuleImg());
        return productDetailsVO;
    }


}
